package main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBWorker implements AutoCloseable{
	private final static String dbLocation = "d:/TestDB.mdb";
	Connection connection;
	Statement stmt;
	private ResultSet rs;
	
	public void connectDB() {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			String connURL = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="
					+ dbLocation;
			try {
				connection = DriverManager.getConnection(connURL);
				stmt = connection.createStatement();
			} catch (Exception err) {
				System.out.println("ERROR: " + err.getMessage());
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
	
	public void executeVoid(String query) {
		try {
			stmt.execute(query);
		} catch (SQLException e) {
			System.out.println("ERROR: "+e.toString());
		}
	}
	
	public ResultSet execute(String query) {
		try {
			stmt.execute(query);
			rs = stmt.getResultSet(); 
		} catch (SQLException e) {
			System.out.println("ERROR: " + e.toString());
		} 
		return rs;
	}
	
	public void close() {
		try {
			rs.close();
			stmt.close();
			connection.close();
		} catch (SQLException e) {
			System.out.println("ERROR: "+e.toString());
		}
		
	}
}
